//
//  HHOAuthController.m
//  oschina
//
//  Created by hjl on 16/3/31.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHOAuthController.h"
#import "HHAccount.h"
#import "HHAccountTool.h"
#import <MJExtension.h>

@interface HHOAuthController () <UIWebViewDelegate>

@end

@implementation HHOAuthController

- (void)loadView{
    UIWebView *web = [[UIWebView alloc]init];
    web.scrollView.bounces = NO;
    self.view = web;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 加载登录界面
    [self loadWebView];
}

- (void)loadWebView{
    //1. 地址
    NSString *strUrl = @"https://www.oschina.net/action/oauth2/authorize?response_type=code&client_id=tNPjZNJaJ4RTPclhlYAm&redirect_uri=http://www.baidu.com&response_type=code";
    NSURL *url = [NSURL URLWithString:strUrl];
    //2. 请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //3. 加载
    UIWebView *webView = (UIWebView *)self.view;
    webView.delegate = self;
    [webView loadRequest:request];
}


#pragma -webView delegate
// 每次webview发送请求时,都会调用,返回yes代表允许加载,返回no代表不允许加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *strUrl = request.URL.absoluteString;
    NSRange range = [strUrl rangeOfString:@"code="];
    if (range.location != NSNotFound) {
        NSString *code = [strUrl substringFromIndex:(range.length + range.location)];
        //换取accessToKen
        [self accessToKenWithCode:code];
        return NO;
    }
    return YES;
}

#pragma -换取accessToKen
- (void)accessToKenWithCode:(NSString *)code{
/*
 parameters[@"client_id"] = @"tNPjZNJaJ4RTPclhlYAm";
 parameters[@"client_secret"] = @"qdhJvSZ8cSfuyRGr4yvDW9BThde1d1XA";
 parameters[@"grant_type"] = @"authorization_code";
 parameters[@"redirect_uri"] = @"http://www.baidu.com";
 parameters[@"code"] = code;
 parameters[@"dataType"] = @"json";
*/
    NSString *strUrl = [NSString stringWithFormat:@"https://www.oschina.net/action/openapi/token?client_id=tNPjZNJaJ4RTPclhlYAm&client_secret=qdhJvSZ8cSfuyRGr4yvDW9BThde1d1XA&grant_type=authorization_code&redirect_uri=http://www.baidu.com&code=%@&dataType=json",code];
    
    // 获得NSURLSession对象
    NSURLSession *session = [NSURLSession sharedSession];
    
    // 创建任务
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:strUrl] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        /*
        refresh_token : 898cd35d-49a2-44f9-b25d-2839d70f59ef,
        uid : 1753722,
        token_type : bearer,
        access_token : 93d6fbc8-6a31-4a36-902b-583d90ded700,
        expires_in : 604799
         */
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
       // HHLog(@"%@", HHDoc);
        HHAccount *account = [HHAccount mj_objectWithKeyValues:dict];
        [HHAccountTool saveAccount:account];
    }];
    // 启动任务
    [task resume];
}

@end
